﻿using System;
using System.Data.Linq;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MyCalManager.Data;
using MyCalManager.Helper;
using MyCalManager.Model;
using System.Collections.Generic;
using WPFLocalization;

namespace MyCalManager.ViewModel
{
    
    public class NewDayViewModel : ViewModelBase
    {
        private CoreData data = CoreData.Instance;
        private bool CanExecute = false;
        private bool CanUpdate = false;
        private bool Updated = false;
        private List<DateTime> list;
        
        public NewDayViewModel()
        {
            Initialize();
        }

        private void Initialize()
        {
            this.list = this.data.DayList.Select(s => s.Timekey).ToList();
            this.selectedDate = DateTime.Now;//this.list.Last();

            this.Ok = new RelayCommand(
                () => OnOk(),
                () => CanExecute);

            this.Cancel = new RelayCommand(
                () => OnCancel());

            this.UpdateDay = new RelayCommand(
               () => OnUpdate(),
               () => CanUpdate);
        }

        

        private void SetCanExecute()
        {
            bool x = false;
            if (this.selectedDate != null)
            {
                if (!this.list.Contains(this.selectedDate))
                {
                    x = true;
                    this.Orig = string.Empty;
                    this.CanUpdate = false;
                }
                else
                {
                    decimal orig = this.data.DayList.Where(s => s.Timekey == this.selectedDate).Select(s => s.Gewicht).Single();
                    this.Orig = string.Format(LocalizationManager.ResourceManager.GetObject("Orig").ToString(),orig);
                    decimal o;
                    if (decimal.TryParse(this.Gewicht, out o))
                    {
                        if (o != orig)
                        {
                            this.CanUpdate = weightValid;
                        }
                        else
                        {
                            this.CanUpdate = false;
                        }
                    }
                    else
                    {
                        this.CanUpdate = false;
                    }
                }
            }
            this.CanExecute = x && weightValid;
        }

        #region Commands and CommandMethods

        public RelayCommand Ok
        {
            get;
            private set;
        }

        public RelayCommand Cancel
        {
            get;
            private set;
        }

        public RelayCommand UpdateDay
        {
            get;
            private set;
        }

        private void OnCancel()
        {
            Navigator n = new Navigator();
            n.To = NavigateTo.Main;
            if (Updated)
                n.Result = NavigateResult.DayUpdated;
            else
                n.Result = NavigateResult.Cancel;
            Messenger.Default.Send(n);
        }

        private void OnOk()
        {
            Day d = new Day();

            DateTime selectedDay = new DateTime(this.selectedDate.Year, this.selectedDate.Month, this.selectedDate.Day, 0, 0, 0);
            d.Timekey = selectedDay;
            d.UserID = this.data.UserID;
            d.Gewicht = decimal.Parse(this.Gewicht);
            d.SumKal = 0;
            d.SumWWPoints = 0;
            if (this.data.AddDay(d))
            {
                this.list = this.data.DayList.Select(s => s.Timekey).ToList();
                this.Gewicht = string.Empty;
                Navigator n = new Navigator();
                n.To = NavigateTo.Main;
                n.Result = NavigateResult.DayAdded;
                Messenger.Default.Send(n);
            }
        }

        private void OnUpdate()
        {
            if (this.data.UpdateDay(this.selectedDate, decimal.Parse(this.Gewicht)))
            {
                SetCanExecute();
                Updated = true;
            }
        }

        #endregion


        #region Properties

        public const string selectedDatePropertyName = "selectedDate";
        private DateTime _myselectedDate = DateTime.Now;
        public DateTime selectedDate
        {
            get
            {
                return _myselectedDate;
            }

            set
            {
                if (_myselectedDate == value)
                {
                    return;
                }

                _myselectedDate = value;
                SetCanExecute();

                // Update bindings, no broadcast
                RaisePropertyChanged(selectedDatePropertyName);

            }
        }

       
        public const string weightValidPropertyName = "weightValid";
        private bool _myweightValid = false;
        public bool weightValid
        {
            get
            {
                return _myweightValid;
            }

            set
            {
                if (_myweightValid == value)
                {
                    return;
                }
                
                _myweightValid = value;

                SetCanExecute();

                // Update bindings, no broadcast
                RaisePropertyChanged(weightValidPropertyName);
                
            }
        }

        
        public const string GewichtPropertyName = "Gewicht";
        private string _myGewicht = string.Empty;
        public string Gewicht
        {
            get
            {
                return _myGewicht;
            }

            set
            {
                if (_myGewicht == value)
                {
                    return;
                }

                _myGewicht = value;

                SetCanExecute();

                // Update bindings, no broadcast
                RaisePropertyChanged(GewichtPropertyName);

            }
        }

        public const string OrigPropertyName = "Orig";
        private string _myOrig = string.Empty;
        public string Orig
        {
            get
            {
                return _myOrig;
            }

            set
            {
                if (_myOrig == value)
                {
                    return;
                }

                _myOrig = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(OrigPropertyName);

            }
        }

        #endregion
    }
}